﻿using System;
using WP7Contrib.Collections;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;

namespace CDFWPUG.Helpers
{
    public class TwitterHelper
    {
        public static ObservableCollection<Model.Tweet> ParseTweets(string xml)
        {

            XElement xmlTweets = XElement.Parse(xml);
            List<Model.Tweet> results = (from tweet in xmlTweets.Elements("{http://www.w3.org/2005/Atom}entry")
                                   select new Model.Tweet
                                         { 
                                             Message = tweet.Element("{http://www.w3.org/2005/Atom}title").Value.Trim(),
                                             Username = tweet.Element("{http://www.w3.org/2005/Atom}author").Element("{http://www.w3.org/2005/Atom}name").Value.Split(' ')[0],
                                             ImageSource = (from t2 in tweet.Elements("{http://www.w3.org/2005/Atom}link")
                                                            where t2.Attribute("type").Value.Contains("image")
                                                            select t2.Attribute("href").Value).First(),
                                             PublishedDate = Convert.ToDateTime(tweet.Element("{http://www.w3.org/2005/Atom}published").Value.Trim())
                                         }).ToList();


            return new ObservableCollection<Model.Tweet>(results);

        }

        internal static ObservableCollection<Model.Tweet> Test_GetTweets()
        {
            ObservableCollection<Model.Tweet> list = new ObservableCollection<Model.Tweet>();
            list.Add(new Model.Tweet{Message= "Test1", Username="Bugail", ImageSource = ""});
            list.Add(new Model.Tweet { Message = "Test2", Username = "Bugail", ImageSource = "" });
            list.Add(new Model.Tweet { Message = "Test3", Username = "Bugail", ImageSource = "" });
            list.Add(new Model.Tweet { Message = "Test4", Username = "Bugail", ImageSource = "" });
            list.Add(new Model.Tweet { Message = "Test5", Username = "Bugail", ImageSource = "" });
            return list;
        }
    }
}
